package com.zjhd.sealcutting.service.impl;

import com.google.common.collect.Maps;
import com.zjhd.sealcutting.dao.UserInfoMapper;
import com.zjhd.sealcutting.dao.WorksInfoMapper;
import com.zjhd.sealcutting.domain.CreativeTask;
import com.zjhd.sealcutting.domain.UserInfo;
import com.zjhd.sealcutting.domain.WorksInfo;
import com.zjhd.sealcutting.param.CreativeTaskSearchQuery;
import com.zjhd.sealcutting.service.CreativeTaskService;
import com.zjhd.sealcutting.utils.DateUtil;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import com.zjhd.sealcutting.vo.CreativeTaskVo;
import com.zjhd.sealcutting.vo.SearchResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjhd.sealcutting.dao.CreativeTaskMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO  创作任务
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/26 15:49
 */
@Service
public class CreativeTaskServiceImpl implements CreativeTaskService {

    @Autowired
    private CreativeTaskMapper creativeTaskMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private WorksInfoMapper worksInfoMapper;

    /**
     * TODO 分页查询创作任务
     * @author wangchao
     * @date 2020/11/26 16:00
     * @param
     * @return
     */
    @Override
    public SearchResult queryCreativeTaskParamlist(CreativeTaskSearchQuery param) throws Exception {
        //定义map 封装查询参数到map中
        Map<String, Object> map = Maps.newLinkedHashMap();
        if(param.getUserId()!=null){
            UserInfo userInfo=userInfoMapper.selectByPrimaryKey(param.getUserId());
            if(userInfo!=null && !userInfo.getUserGroup().equals("1")){
                map.put("distributionId",param.getUserId());
            }
        }
        if(param.getExhibitionId()!=null){
            map.put("exhibitionId",param.getExhibitionId());
        }
        //当前页  分页数
        map.put("pageIndex", (Integer.parseInt(param.getPageIndex()) - 1) * Integer.parseInt(param.getPageSize()));
        map.put("pageSize", Integer.parseInt(param.getPageSize()));
        List<CreativeTaskVo> creativeTasks=creativeTaskMapper.queryParamCreativeTaskList(map);
        long totalCount=creativeTaskMapper.queryParamCreativeTaskListCount(map);
        if(creativeTasks.size()>0){
            return new SearchResult.Builder<List<CreativeTaskVo>>()
                    .setCurrent(Integer.parseInt(param.getPageIndex()))
                    .setPageSize(Integer.parseInt(param.getPageSize()))
                    .setTotalCount(totalCount)
                    .setPagedItems(creativeTasks == null ? new ArrayList() : creativeTasks)
                    .build();
        }else {
            return new SearchResult.Builder().setPagedItems(new ArrayList()).build();
        }
    }

    /**
     * TODO 新增创作任务
     * @author wangchao
     * @date 2020/11/26 16:00
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult insertCreativeTask(CreativeTask creativeTask) throws Exception {
        try {
            creativeTask.setCreateTime(DateUtil.getCurrentTime());
            creativeTask.setState(0);
            int i=creativeTaskMapper.insert(creativeTask);
            if(i>0){
                return ResultGenerator.genSuccessResult("新增创作任务成功!");
            }else {
                return ResultGenerator.genFailResult("新增创作任务失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("新增创作任务失败!");
    }

    /**
     * TODO 查询创作任务详情
     * @author wangchao
     * @date 2020/11/26 16:00
     * @param
     * @return
     */
    @Override
    public ResponseResult queryByIdDetail(Integer id) throws Exception {
        try {
            return ResultGenerator.genSuccessResult(creativeTaskMapper.selectByPrimaryKey(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询创作任务详情失败");
        }
    }

    /**
     * TODO  投稿任务
     * @author wangchao
     * @date 2020/11/26 16:03
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult contributionTask(Integer id,String workId) throws Exception {
        try {
            CreativeTask creativeTask=new CreativeTask();
            creativeTask.setId(id);
            creativeTask.setState(1);
            creativeTask.setWorkId(workId);
            int i=creativeTaskMapper.updateByPrimaryKeySelective(creativeTask);
            if(i>0){
                //更改对应作品状态
                WorksInfo worksInfo=new WorksInfo();
                worksInfo.setId(workId);
                worksInfo.setWorkStatus(1);
                worksInfoMapper.updateByPrimaryKeySelective(worksInfo);
                return ResultGenerator.genSuccessResult("投稿创作任务成功!");
            }else {
                return ResultGenerator.genFailResult("投稿创作任务失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("投稿创作任务失败!");
    }

    /**
     * TODO 修改创作任务
     * @author wangchao
     * @date 2020/12/14 10:37
     * @param
     * @return
     */
    @Override
    public ResponseResult updateCreativeTask(CreativeTask creativeTask) throws Exception {
        try {
            int i=creativeTaskMapper.updateByPrimaryKeySelective(creativeTask);
            if(i>0){
                return ResultGenerator.genSuccessResult("修改创作任务成功!");
            }else {
                return ResultGenerator.genFailResult("修改创作任务失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("修改创作任务失败!");
    }

    /**
     * TODO 查询作品任务所关联的所有展览
     * @author wangchao
     * @date 2020/12/29 14:25
     * @param
     * @return
     */
    @Override
    public List<CreativeTask> queryAllExhibition() throws Exception {
        return creativeTaskMapper.queryAllExhibition();
    }
}
